package org.jeecg.modules.pos.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.pos.entity.PosOrderTeam;
import org.jeecg.modules.pos.mapper.PosOrderTeamMapper;
import org.jeecg.modules.pos.service.IPosOrderTeamService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PosOrderTeamImpl extends ServiceImpl<PosOrderTeamMapper,PosOrderTeam> implements IPosOrderTeamService {
    @Override
    public PosOrderTeam addBusTeams(String hotelId, String orderId) {
        PosOrderTeam team = new PosOrderTeam();
        List<PosOrderTeam> existTeams = list(Wrappers.<PosOrderTeam>query().eq("is_delete", 0).eq("hotel_id", hotelId)); // 1团队房，2联房
        Integer sortIndex = 1;
        List<Integer> indexs = existTeams.stream().map(s->s.getSortIndex()).collect(Collectors.toList());
        while(true){
            Integer finalSortIndex = sortIndex;
            if(indexs.stream().filter(s->s.equals(finalSortIndex)).count() == 0) {
                break;
            }
            sortIndex++;
        }
        team.setOrderId(orderId);
        team.setHotelId(hotelId);
        team.setSortIndex(sortIndex);
        save(team);
        return team;
    }
}
